package a66weding.com.jiehuntong.wx;

import android.content.Context;

import android.util.Log;
import android.widget.Toast;

import com.tencent.mm.sdk.modelpay.PayReq;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;


import java.util.Random;

/**
 * Created by admin on 2016/8/26.
 */
public class WXPayManger {
    private  Context context;
    static PayReq req;
    IWXAPI msgApi;
    public WXPayManger(Context context,String prepayId){
        this.context = context;
        msgApi = WXAPIFactory.createWXAPI(context,Constants.APP_ID);
        req = new PayReq();
        //启动微信支付;
        genPayReq(prepayId);
    }


   public  void sendPayReq() {
       if(!msgApi.isWXAppInstalled()){
           Toast.makeText(context,"微信未安装，不能启动微信支付", Toast.LENGTH_SHORT).show();
           return;
       }
       if(!msgApi.isWXAppSupportAPI()){
           Toast.makeText(context,"你的微信过低，不支持支付",Toast.LENGTH_SHORT).show();
           return;
       }
        msgApi.registerApp(Constants.APP_ID);
       //调用微信支付 ;
        msgApi.sendReq(req);

    }

    /**
     * 获取支付订单;
     */
    private void genPayReq(String prepayId ) {

        req.appId =Constants.APP_ID;

        req.partnerId =Constants.MCH_ID;
        //从网络获取的预订单好;
        req.prepayId= prepayId;

        req.packageValue = "Sign=WXPay";

        req.nonceStr= genNonceStr();
        //时间
        req.timeStamp= ""+genTimeStamp();
        //支付宝秘钥
        req.sign= Constants.SIGN;

    }

    private String genNonceStr() {
        Random random = new Random();
        return MD5.getMessageDigest(String.valueOf(random.nextInt(10000)).getBytes());
    }

    private  long genTimeStamp() {
        return System.currentTimeMillis() / 1000;
    }







}
